package com.example.booktest.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.booktest.R;
import com.example.booktest.entity.CatalogEntity;

import java.util.List;

public class CatalogAdapter extends RecyclerView.Adapter<CatalogAdapter.CatalogViewHolder> {

    private List<CatalogEntity.ResultDTO> catalogList;

    public CatalogAdapter() {
        // 初始化catalogList为一个空的ArrayList以避免null值
        this.catalogList = java.util.Collections.emptyList();
    }

    @NonNull
    @Override
    public CatalogViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_catalog, parent, false);
        return new CatalogViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull CatalogViewHolder holder, int position) {
        CatalogEntity.ResultDTO catalog = catalogList.get(position);
        holder.bind(catalog);
    }

    @Override
    public int getItemCount() {
        return catalogList.size();
    }

    public void setCatalogList(List<CatalogEntity.ResultDTO> catalogList) {
        this.catalogList = catalogList;
        notifyDataSetChanged();
    }

    public class CatalogViewHolder extends RecyclerView.ViewHolder {
        private TextView tvCatalog;

        public CatalogViewHolder(View itemView) {
            super(itemView);
            tvCatalog = itemView.findViewById(R.id.tvCatalog);
        }

        public void bind(CatalogEntity.ResultDTO catalog) {
            tvCatalog.setText(catalog.getCatalog());
        }
    }
}